package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.collection.SparseArrayCompat;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.ExamCategory;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.fragment.CreateChoiceExamFragment;
import com.zahb.qadx.util.ActivityManager;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.JsonUtil;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.RequestBody;

public class CreateChoiceExamActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().addActivity(ActivityManager.KEY_CREATE_CHOICE_EXAM, this);
        super.onCreate(savedInstanceState);
        getExamCategoryList();
    }

    @Override
    protected void onDestroy() {
        ActivityManager.getInstance().removeActivity(ActivityManager.KEY_CREATE_CHOICE_EXAM, this);
        super.onDestroy();
    }

    @Override
    protected int getLayout() {
        return 0;
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.create_choice_exam;
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, CreateChoiceExamActivity.class);
        context.startActivity(intent);
    }

    private void getExamCategoryList() {
        showProgressDialog("请稍等...");
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", 1);
        params.put("pageSize", 10);
        RequestBody body = JsonUtil.getRequestBody(params);
        addDisposable(RetrofitService.getNetService()
                .getExamCategoryList(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        ArrayList<ExamCategory> examCategoryList = parseExamCategoryData(response.getData().getList());
                        if (examCategoryList != null) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelableArrayList("examCategoryList", examCategoryList);
                            CreateChoiceExamFragment fragment = new CreateChoiceExamFragment();
                            fragment.setArguments(bundle);
                            getSupportFragmentManager().beginTransaction()
                                    .add(R.id.container, fragment, "CreateChoiceExamFragment")
                                    .commitAllowingStateLoss();
                        }
                    } else if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    Tips.RequestError(getActivity());
                }));
    }

    private ArrayList<ExamCategory> parseExamCategoryData(List<ExamCategory> categoryList) {
        if (categoryList == null) return null;

        SparseArrayCompat<ExamCategory> arrayLevel1 = new SparseArrayCompat<>();
        SparseArrayCompat<ExamCategory> arrayLevel2 = new SparseArrayCompat<>();
        for (ExamCategory category : categoryList) {
            if (category.getLevel() == 1)
                arrayLevel1.put(category.getId(), category);
            if (category.getLevel() == 2)
                arrayLevel2.put(category.getId(), category);
        }

        for (int i = 0; i < arrayLevel2.size(); i++) {
            ExamCategory categoryChild = arrayLevel2.valueAt(i);
            ExamCategory categoryParent = arrayLevel1.get(categoryChild.getParentId());
            if (categoryParent != null) {
                List<ExamCategory> childList = categoryParent.getChildList();
                if (childList == null)
                    childList = new ArrayList<>();
                childList.add(categoryChild);
                categoryParent.setChildList(childList);
            }
        }

        ArrayList<ExamCategory> examCategoryList = new ArrayList<>();
        for (int i = 0; i < arrayLevel1.size(); i++) {
            examCategoryList.add(arrayLevel1.valueAt(i));
        }
        return examCategoryList;
    }
}
